package com.example.auth.response;

import lombok.Data;;

import java.io.Serializable;

/**
 * @author 13057
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回处理消息
     */
    private String msg = "操作成功！";

    /**
     * 返回代码
     */
    private Integer code = 200;

    /**
     * 返回数据对象 data
     */
    private T data;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public Result() {

    }

    public Result<T> SUCCESS(String message) {
        this.msg = message;
        this.code = 200;
        this.success = true;
        return this;
    }

    public static<T> Result<T> OK() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(200);
        r.setMsg("成功");
        return r;
    }

    public static<T> Result<T> OK(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(200);
        r.setData(data);
        return r;
    }

    public static<T> Result<T> OK(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(200);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static Result<Object> ERROR(String msg) {
        return error(500, msg);
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMsg(msg);
        r.setSuccess(false);
        return r;
    }
    public static Result<Object> error( String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(500);
        r.setMsg(msg);
        r.setSuccess(false);
        return r;
    }


    /**
     * 无权限访问返回结果
     */
    public static Result<Object> noAuth(String msg) {
        return error(401, msg);
    }

}